<template>
  <div>
    <a-space>
      <a-select
        :value="values"
        optionFilterProp="label"
        show-search
        placeholder=""
        style="width: 150px"
        @change="handerChange"
        :disabled="disabled"
      >
        <a-select-option v-for="item in options" :key="item.id" :value="item.id" :label="item.name">{{
          item.name
        }}</a-select-option>
      </a-select>
      <a-popconfirm placement="bottomLeft" ok-text="确定" cancel-text="取消" @confirm="confirm" :disabled="disabled">
        <template #title>
          <div class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchQuery">
              <a-row :gutter="24">
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <a-form-item label="仓库">
                    <j-dict-select-tag
                      placeholder="请选择仓库"
                      v-model="queryParam.wareHouseId"
                      dictCode="erp_warehouse,name,id"
                    />
                  </a-form-item>
                </a-col>
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <a-form-item label="库位编号">
                    <a-input placeholder="请输入库位编号" v-model="queryParam.code"></a-input>
                  </a-form-item>
                </a-col>
                <template v-if="toggleSearchStatus">
                  <a-col :xl="6" :lg="7" :md="8" :sm="24">
                    <a-form-item label="库位名称">
                      <a-input placeholder="请输入库位名称" v-model="queryParam.name"></a-input>
                    </a-form-item>
                  </a-col>
                  <!--                  <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
                  <!--                    <a-form-item label="状态">-->
                  <!--                      <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="erp_status"/>-->
                  <!--                    </a-form-item>-->
                  <!--                  </a-col>-->
                </template>
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                    <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                    <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                    <a @click="handleToggleSearch" style="margin-left: 8px">
                      {{ toggleSearchStatus ? '收起' : '展开' }}
                      <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                    </a>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <!-- 查询区域-END -->

          <!-- 操作按钮区域 -->
          <div class="table-operator"></div>
          <!-- table区域-begin -->
          <div>
            <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
              <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
              <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
              >项
              <a style="margin-left: 24px" @click="onClearSelected">清空</a>
            </div>
            <a-table
              ref="table"
              size="middle"
              :scroll="{ x: true }"
              bordered
              rowKey="id"
              :columns="columns"
              :dataSource="dataSource"
              :pagination="ipagination"
              :loading="loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
              class="j-table-force-nowrap1"
              @change="handleTableChange"
            >
            </a-table>
          </div>
        </template>
        <span style="width: 100px; height: auto"> <antd-icon type="search-o" /></span>

        <!--        <a-button>BL</a-button>-->
      </a-popconfirm>
    </a-space>
  </div>
</template>

<script>
import Vue from 'vue'
import { AntDesignOutline, DashboardOutline, TwitterOutline, SearchOutline } from '@ant-design/icons'
import AntdIcon from '@ant-design/icons-vue'
AntdIcon.add(AntDesignOutline, DashboardOutline, SearchOutline)
Vue.use(AntdIcon)

import ErpLocationList from '@/views/modules/location/ErpLocationList'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { mixinDevice } from '@/utils/mixin'
import { getAction } from '@api/manage'
import { ajaxGetDictItems } from '@api/api'

export default {
  name: 'TslWarehouse',
  describes: '仓库',
  props: {
    value: {
      type: String,
      required: false,
      default: '',
    },
    rowData: {
      type: Object,
      required: false,
    },
    warehouseCode: {
      type: String,
      required: false,
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
    // list: {
    //   type: Array,
    //   required: true
    // },
    // currentStatus: {
    //   type: Number,
    //   default: 0,
    //   required: false
    // }
  },
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ErpLocationList,
  },
  data() {
    return {
      values: this.value,
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '仓库编号',
          align: 'center',
          dataIndex: 'warehouseCode',
        },
        {
          title: '仓库名称',
          align: 'center',
          dataIndex: 'warehouseName',
        },
        {
          title: '库位编号',
          align: 'center',
          dataIndex: 'code',
        },
        {
          title: '库位名称',
          align: 'center',
          dataIndex: 'name',
        },
      ],
      statusColorMap: {
        启用: 'green',
        停用: 'red',
      },
      url: {
        list: '/base/erpLocation/list',
        delete: '/base/erpLocation/delete',
        deleteBatch: '/base/erpLocation/deleteBatch',
        exportXlsUrl: '/base/erpLocation/exportXls',
        importExcelUrl: 'base/erpLocation/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
      list1: [],
      options: [
        { value: 'jack', label: 'Jack' },
        { value: 'lucy', label: 'Lucy' },
        { value: 'tom', label: 'Tom' },
      ],
    }
  },
  created() {
    this.loadDictData()
  },
  mounted() {
    this.$nextTick(() => {
      console.log(this.$refs.location)
    })
    console.log(this.rowData)
  },
  computed: {},
  methods: {
    loadData(arg) {
       this.dataSource = []
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      if (this.warehouseCode != null) {
        params.warehouseCode = this.warehouseCode
      }
      this.loading = true
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
             this.options = res.result.records
            this.dataSourceBack = this.dataSource
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    loadDictData() {
      let param = null
      if (this.warehouseCode != null) {
        param = {
          warehouseCode: this.warehouseCode,
        }
      }
      ajaxGetDictItems('erp_location,name,id', param).then((res) => {
        if (res.success) {
          this.options = res.result
        }
      })
    },
    confirm() {
      if (this.selectionRows.length > 0) {
        const val = this.selectedRowKeys[0]
        this.values = val
        this.$emit('input', val)
        this.getLocationInfo(val)
      }
      this.onClearSelected()
    },
    handerChange(value, option) {
      this.values = value
      this.$emit('input', value)
      this.getLocationInfo(value)
    },
    getLocationInfo(value) {
      const { row } = this.rowData
      const param = { id: value }
      getAction('/base/erpLocation/queryInfoById', param).then((res) => {
        if (res.success) {
          // 向外传递
          this.$emit('handlerOk', {
            result: res.result,
            row: row,
          })
        }
      })
    },
    add() {
      this.showModal()
    },
    showModal() {
      this.visible = true
      this.queryParam = { procurementType: this.procurementType }
      this.loadData()
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      // fieldList.push({type:'string',value:'wareHouseId',text:'仓库',dictCode:"erp_warehouse,name,id"})
      fieldList.push({ type: 'string', value: 'code', text: '库位编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'name', text: '库位名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'status', text: '状态', dictCode: 'erp_status' })
      this.superFieldList = fieldList
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      this.$emit('handlerOk', this.selectionRows[0])
      this.close()
    },
    close() {
      // this.searchReset(false);
      this.queryParam = {}
      this.selectedRowKeys = []
      this.selectionRows = []
      this.visible = false
    },
  },
  watch: {
    // 当message变化时，这个函数就会被调用
    warehouseCode(newValue, oldValue) {
      console.log(`消息从 "${oldValue}" 变为 "${newValue}"`)
      console.log(this.options)
      this.loadData()
      // 这里可以执行更多的逻辑
    },
  },
}
</script>

<style scoped>
/* 覆盖Dropdown菜单项在悬停时的背景颜色 */
.ant-dropdown-menu-item:hover,
.ant-dropdown-menu-submenu-title:hover {
  background-color: inherit; /* 或者设置为初始背景色 */
}
</style>